package com.cash.channel.web.controller;

import com.cash.channel.web.annotation.RestControllerMapper;
import com.cash.channel.web.annotation.ServiceLog;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.channel.ChannelModel;
import com.cash.channel.api.service.ApiService;
import com.cash.channel.api.service.ChannelService;
import com.cash.channel.web.model.RequestModel;
import com.cash.channel.web.signature.AESTool;
import com.cash.channel.web.signature.RSATool;
import com.cash.channel.web.signature.SignatureTool;
import com.cash.common.model.CodeEnum;
import com.cash.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Slf4j
@RestControllerMapper("/channel")
public class ChannelController extends BaseController {

    @Autowired
    private ChannelService channelService;

    @Resource
    private ApiService cashChannelApiService;


    @PostMapping("/add")
    @ServiceLog("生成新渠道")
    public Result channelAdd(ChannelModel channelModel) {
        try {
            return channelService.saveChannelInfoByCode(channelModel);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/test/ChANaldAtacrEAt")
    @ServiceLog("生成渠道数据")
    public Result channelTest(@RequestBody RequestModel requestModel) {
        try {
            Result<ChannelModel> channelModelResult = cashChannelApiService.getChannelInfoByChannelId(requestModel.getChannel());
            if (CodeEnum.ERROR.getCode().equals(channelModelResult.getCode())) {
                log.error("查询渠道["+requestModel.getChannel()+"]信息失败："+channelModelResult.getMsg());
                throw new CashChannelException("查询渠道["+requestModel.getChannel()+"]信息失败");
            }
            ChannelModel channelModel = channelModelResult.getData();
            RequestModel testData = new RequestModel();
            testData.setChannel(requestModel.getChannel());
            testData.setEncryptKey(Base64.getEncoder().encodeToString(RSATool.rsaEncryptByPrivateKey(RSATool.parseRSAPrivateKey(channelModel.getcChannelPrivateKey()),
                    channelModel.getcChannelAesKey().getBytes(StandardCharsets.UTF_8))));
            testData.setEncrypt(1);
            testData.setCompress(0);
            testData.setData(Base64.getEncoder().encodeToString(AESTool.aesEncrypt(requestModel.getData().getBytes(StandardCharsets.UTF_8), channelModel.getcChannelAesKey())));
            testData.setSign(Base64.getEncoder().encodeToString(RSATool.rsaSignByPrivateKey(RSATool.parseRSAPrivateKey(channelModel.getcChannelPrivateKey()),
                    SignatureTool.buildSignContent(testData).getBytes(StandardCharsets.UTF_8))));
            return Result.succeed(testData);
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/test/ChANaldAtacrEAttWo")
    @ServiceLog("分析渠道数据")
    public Result channelTestTwo(@RequestBody RequestModel requestModel) {
        try {
            Result<ChannelModel> channelModelResult = cashChannelApiService.getChannelInfoByChannelId(requestModel.getChannel());
            if (CodeEnum.ERROR.getCode().equals(channelModelResult.getCode())) {
                log.error("查询渠道["+requestModel.getChannel()+"]信息失败："+channelModelResult.getMsg());
                throw new CashChannelException("查询渠道["+requestModel.getChannel()+"]信息失败");
            }
            ChannelModel channelModel = channelModelResult.getData();
            byte[] originDataBytes = AESTool.aesDecrypt(org.apache.commons.codec.binary.Base64.decodeBase64(requestModel.getData()), channelModel.getcPlatformAesKey());
            return Result.succeed(new String(originDataBytes, StandardCharsets.UTF_8));
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.failed(e.getMessage());
        }
    }

}
